let testId = '',
  teacher = '';

window.addEventListener('DOMContentLoaded', () => {
  isTeaLogin();
  testId = localStorage.getItem('testId');
  teacher = localStorage.getItem('teacher');
});

function loadAdminPage() {
  loadApList(testId, '');
}

function loadApList(testId, clazz) {
  $.get(`${app.urlLocation.queryTpDetail}`, {testId: testId, clazz: clazz}, res => {
    let subject = res.apList[0];
    $('.paper-title').text(subject.testTitle);
    $('.paper-class-type').text(subject.classType);
    writeApCard(res.apList, res.percent);
  })
}

function writeApCard(apList, percent) {
  let subjectHtml = '',
    index = 0;
  $.each(apList, (i, v) => {
    let subjectType = v.subjectType;
    subjectHtml += returnSubjectHeader(v);
    switch (subjectType) {
      case 1:
      case 2:
        subjectHtml += returnOption(v, percent[index]);
        index++;
        break;
      case 3:
        subjectHtml += returnOpen();
        break;
    }
    subjectHtml += `</div></div>`;
  });
  $('.test-content').html(subjectHtml);
  app.resetForm();
}

function returnSubjectHeader(subject) {
  return `<div class="layui-card">` +
    `<div class="layui-card-header">` +
    `<span class="layui-form-label">第` +
    `<span class="question-no">${subject.testNo}</span>题</span>` +
    `<span class="question-content">${subject.subjectTitle}</span>` +
    `<span class="answer-percent">答案占比</span>` +
    `<span class="question-type layui-hide layui-clear">${subject.subjectType}</span></div>` +
    `<div class="layui-card-body">`;
}

function returnOption(subject, percent) {
  let html = '<div class="option-box">';
  let options = subject.subjectContent.split('|');
  let optionNumber = 65;
  let percentNumber = 0.0;
  let sum = 0;
  for (let i in percent) {
    sum += percent[i];
  }
  sum = sum === 0 ? 1 : sum;
  for (let i = 0; i < options.length; i++) {
    percentNumber = percent[i] / sum * 100;
    html += '<div class="layui-input-block">' +
      '<span class="option1">' + String.fromCharCode(optionNumber + i) + '.&nbsp;&nbsp;' + options[i] + '</span>' +
      '<span class="percent">' + percentNumber.toFixed(2) + '%</span></div>';
  }
  html += '</div>';
  return html;
}

function returnOpen() {
  return '<div class="details"><p class="answer">此题为开放题，不计入总成绩!<span></span></p></div>';
}

function returnPreviousPage() {
  localStorage.removeItem('testId');
  window.location.href = '/teacher-action/check';
}

function returnPreviousPageToTest() {
  localStorage.removeItem('testId');
  window.location.href = '/teacher-action/test';
}